/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.webmanager.controller;


import com.alibaba.fastjson.JSON;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.MapConstant;
import com.tbit.uqbike.constant.SysConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.Warehouse;
import com.tbit.uqbike.object.pojo.vo.StockMachineVO;
import com.tbit.uqbike.object.pojo.vo.WarehouseVo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.base.TokenService;
import com.tbit.uqbike.service.base.UtilsService;
import com.tbit.uqbike.service.business.StockMachineService;
import com.tbit.uqbike.service.business.WarehouseService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.utils.ReflectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库控制层类
 *
 * <p>
 * 1、步骤1 todo <br>
 * 2、步骤2 todo <br>
 * 3、步骤3 todo
 * </p>
 * @author <a href="mailto:zhang.bingrong@tbit.com.cn ">zhangbingrong</a>
 * @since 2024/1/19
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {


    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private StockMachineService stockMachineService;





    /**
     * 根据平台账号id查询
     */
    @RequestMapping("/getByAccountId")
    public JsonResult byAccoutId(Integer mapType, Integer accountId, String warehouseName, @RequestParam(defaultValue = "10") int size,  @RequestParam(defaultValue = "1")int page) {

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<Warehouse> Warehouse = warehouseService.getByAccountId(accountId,warehouseName,size,page);
        int byAccountIdTotal = warehouseService.getByAccountIdTotal(accountId, warehouseName);
        if (Warehouse != null) {
            /** 校准 */
            calibrateService.calibrateWareHouse(Warehouse, mapType);
        }
        return JsonResult.ok(Warehouse,byAccountIdTotal);
    }


    /**
     * 添加
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.WAREHOUSE, methods = MethodEnum.WAREHOUSE_ADD)
    public JsonResult add( Warehouse warehouse, Integer mapType,String token) {
//        AccountUser accountUser = tokenService.getAccount(token);
//        Role role = accountUser.getRole();
//        List<Menu> roleType = roleMenuService.getRoleType(role.getRoleId());
//        //菜单名称
//        List<String> collect = roleType.stream().map(menu -> menu.getMenuName()).collect(Collectors.toList());
//        if (!collect.contains("仓库管理")){
//            return JsonResult.fail(500, "当前人员没有权限");
//        }
        Assert.notNull(warehouse.getAccountId(), "AccountUser.agentId.notNull");

        /**检验*/
        Assert.notNull(warehouse.getWarehouseName(), "ParkPoint.name.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }
        /**保存经纬度*/
        Assert.notNull(warehouse.getLo(), "Map.lon.nutNull");
        Assert.notNull(warehouse.getLa(), "Map.lat.nutNull");
        Point point = new Point(warehouse.getLo(), warehouse.getLa());

        warehouse.setLo(point.getX());
        warehouse.setLa(point.getY());
        /** 保存高德地图原始经纬度 */
        warehouse.setLoC(point.getX());
        warehouse.setLaC(point.getY());

        /** 经纬度校准 */
        calibrateService.calWarehouse(warehouse, mapType, SysConstant.ORIGINAL);

        /** 备注 */
        if (warehouse.getRemark() == null) {
            warehouse.setRemark("");
        }

        Warehouse WarehouseByLo = warehouseService.getNearWarehouse(warehouse.getAccountId(), warehouse.getLo(), warehouse.getLa(),warehouse.getWarehouseName());

        if (WarehouseByLo != null) {
            return JsonResult.fail("当前定位已有仓库或名称重复", TraceUtil.getTraceIdByLocal());
        }
        List<Warehouse> byAccountId = warehouseService.getByAccountId(warehouse.getAccountId(),null,0,365);
        if (byAccountId != null && byAccountId.size() >100 ){
            return JsonResult.fail("仓库已经达到100上线",TraceUtil.getTraceIdByLocal());
        }

        return  warehouseService.insert(warehouse);
    }


    /**
     * 修改仓库
     *
     * @param warehouse
     * @param mapType
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.WAREHOUSE, methods = MethodEnum.WAREHOUSE_UPDATE)
    public JsonResult update( Warehouse warehouse, Integer mapType) {

        /**检验*/
        Assert.notNull(warehouse.getWarehouseId(), "Warehouse.control.warehouseId.notNull");
        Assert.notNull(warehouse.getWarehouseName(), "Warehouse.control.warehouseName.notNull");
        Assert.notNull(warehouse.getAccountId(), "AccountUser.agentId.notNull");

        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        /**查询仓库*/
        Warehouse byWarehouseId = warehouseService.getByWarehouseId(warehouse.getWarehouseId());

        /**替换可修改字段*/
        ReflectUtils.assign(warehouse, byWarehouseId, null);
        warehouse = byWarehouseId;


        /**保存经纬度*/
        Assert.notNull(warehouse.getLo(), "Map.lon.nutNull");
        Assert.notNull(warehouse.getLa(), "Map.lat.nutNull");
        Point point = new Point(warehouse.getLo(), warehouse.getLa());

        warehouse.setLo(point.getX());
        warehouse.setLa(point.getY());
        /** 保存高德地图原始经纬度 */
        warehouse.setLoC(point.getX());
        warehouse.setLaC(point.getY());

        /** 经纬度校准 */
        calibrateService.calWarehouse(warehouse, mapType, SysConstant.ORIGINAL);

        /** 备注 */
        if (warehouse.getRemark() == null) {
            warehouse.setRemark("");
        }

        return warehouseService.update(warehouse);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @OperateLog(module = ModuleEnum.WAREHOUSE, methods = MethodEnum.WAREHOUSE_DEL)
    public JsonResult del( Integer warehouseId) {
        /* 校验 */
        Assert.notNull(warehouseId, "Warehouse.control.warehouseId.notNull");
        List<StockMachineVO> stockMachineVOS = stockMachineService.selectByWarehouseId(warehouseId);
        if (stockMachineVOS != null && stockMachineVOS.size() > 0) {
            return JsonResult.fail("当前仓库还有绑定的入库车辆",TraceUtil.getTraceIdByLocal());
        }
        warehouseService.del(warehouseId);
        return JsonResult.ok();
    }

/*    *//**
     * 根据平台账号id查询
     *//*
    @RequestMapping("/getByAccountId")
    public JsonResult byAccoutId( Integer mapType, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<Warehouse> Warehouse = warehouseService.getByAccountId(accountId);
        if (Warehouse != null) {
            *//** 校准 *//*
            calibrateService.calibrateWareHouse(Warehouse, mapType);
        }

        return JsonResult.succ(Warehouse);
    }*/


    /**
     * 根据定位查询
     */
    @RequestMapping("/getNearWarehouse")
    public JsonResult getNearWarehouse( Warehouse warehouse, Integer mapType) {

        Assert.notNull(warehouse.getAccountId(), "AccountUser.agentId.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        if (warehouse != null) {
            /** 校准 */
            calibrateService.calWarehouse(warehouse, mapType, SysConstant.ORIGINAL);
        }

        Warehouse WarehouseBack = warehouseService.getNearWarehouse(warehouse.getAccountId(), warehouse.getLo(), warehouse.getLa(),null);
        if (WarehouseBack != null) {
            /** 校准 */
            calibrateService.calibrateWareHouseSin(WarehouseBack, mapType);
            return JsonResult.ok(WarehouseBack);
        }
        return JsonResult.fail( "当前定位没有仓库",TraceUtil.getTraceIdByLocal());

    }

    /**
     *  获取以区域为条件的仓库下车辆
     */
    @RequestMapping("/getAllWarehouse")
    public JsonResult getAllWarehouse( Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        List<WarehouseVo> allWarehouse = warehouseService.getAllWarehouse(accountId);
        return JsonResult.ok(allWarehouse);

    }


    /**
     * 根据平台账号id查询
     */
    @RequestMapping("/getByAccountIds")
    public JsonResult getByAccountIds(Integer mapType, @RequestParam("accountIdList") List<Integer> accountIdList, String warehouseName) {
        log.info("accountIdList:{}", JSON.toJSONString(accountIdList));
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<Warehouse> Warehouse = warehouseService.getByAccountIds(accountIdList,warehouseName);
        return JsonResult.ok(Warehouse);
    }





}



